package com.tansun.easycare.rule.ml.algorithm.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.ml.algorithm.entity.FormulaDict;
import com.tansun.easycare.rule.ml.algorithm.entity.ImgVariables;
import com.tansun.easycare.rule.ml.algorithm.entity.MuntiTables;
import com.tansun.easycare.rule.ml.algorithm.entity.PyResponse;
import com.tansun.easycare.rule.ml.algorithm.service.IAlgorthmService;
import com.tansun.easycare.rule.ml.model.entity.MlDataModel;
import com.tansun.easycare.rule.ml.model.service.MlDataModelService;

/**
 * 图片处理控制类
 * @author xch
 * @version 2018-08-24
 */
@Controller
@RequestMapping(value = "/ml/diagram")
public class MLDiagramController extends BaseController{
	public static String DOWNLOAD_URL = Global.getConfig("ssdpyurl")+"/downloadFile";
	
	@Autowired
	private MlDataModelService mlDataModelService;

	@Autowired
	private IAlgorthmService iAlgrthmService;
	
	@ModelAttribute
	public MlDataModel get(@RequestParam(required=false) String id) {
		MlDataModel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mlDataModelService.get(id);
		}
		if (entity == null){
			entity = new MlDataModel();
		}
		return entity;
	}

	@RequestMapping(value = "getConfusionMaxtrix")
	public @ResponseBody JSONObject getConfusionMaxtrix(MlDataModel mlDataModel, ImgVariables imgVariables,RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.getConfusionMatrix(mlDataModel,imgVariables);
		JSONObject confusionMatrix = (JSONObject) resp.getResponse().get("getConfusionMatrix");
		addMessage(redirectAttributes, "获取混淆矩阵成功");
		return confusionMatrix;
	}
	

	@RequestMapping(value = "getRocCurve")
	public @ResponseBody JSONObject getRocCurve(MlDataModel mlDataModel, ImgVariables imgVariables,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getRocCurve(mlDataModel,imgVariables);
		JSONObject rocCurce = (JSONObject)resp.getResponse().get("getRocCurve");
		addMessage(redirectAttributes, "获取roc曲线成功");
		return rocCurce;
	}
	

	@RequestMapping(value = "getLiftCurve")
	public @ResponseBody JSONObject getLiftCurve(MlDataModel mlDataModel, ImgVariables imgVariables,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getLiftCurve(mlDataModel,imgVariables);
		JSONObject liftCurce = (JSONObject)resp.getResponse().get("getLiftCurve");
		addMessage(redirectAttributes, "获取lift曲线成功");
		return liftCurce;
	}
	

	@RequestMapping(value = "getCompareTestData")
	public @ResponseBody JSONObject getCompareTestData(MlDataModel mlDataModel, ImgVariables imgVariables, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getCompareTestData(mlDataModel,imgVariables);
		JSONObject liftCurce = (JSONObject)resp.getResponse().get("getCompareTestData");
		addMessage(redirectAttributes, "获取lift曲线成功");
		return liftCurce;
	}
	
	/**
	 * 获取图片数据，针对特征工程
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getCorrelogramData")
	public @ResponseBody JSONObject getCorrelogramData(MlDataModel mlDataModel,MuntiTables muntiTables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getCorrelogramData(mlDataModel, muntiTables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("getCorrelogramData");
		addMessage(redirectAttributes, "获取图片数据成功");
		return imgData;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/saveFeatureData")
	public @ResponseBody JSONObject saveFeatureData(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.saveFeatureData(mlDataModel);
		JSONObject imgData = (JSONObject)resp.getResponse().get("saveFeatureData");
		addMessage(redirectAttributes, "特征工程数据保存成功");
		return imgData;
	}
	
	/**
	 * 变量设置，针对特征工程
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/setInOutVariable")
	public @ResponseBody JSONObject setInOutVariable(MlDataModel mlDataModel,MuntiTables muntiTables,RedirectAttributes redirectAttributes,Model model) {
		System.out.println(muntiTables);
		PyResponse resp = iAlgrthmService.setInOutVariable(mlDataModel, muntiTables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("setInOutVariable");
		addMessage(redirectAttributes, "特征工程变量设置成功");
		return imgData;
	}

	/**
	 * 数据平衡，针对特征工程
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/dataBanlance")
	public @ResponseBody JSONObject dataBanlance(MlDataModel mlDataModel,MuntiTables muntiTables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.dataBanlance(mlDataModel, muntiTables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("dataBanlance");
		addMessage(redirectAttributes, "特征工程数据平衡成功");
		return imgData;
	}

	/**
	 * 获取图片数据，分析目标列数据
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getTargetVarAnalysis")
	public @ResponseBody JSONObject getTargetVarAnalysis(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		System.out.println(imgVariables);
		PyResponse resp = iAlgrthmService.getTargetVarAnalysis(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("getTargetVarAnalysis");
		addMessage(redirectAttributes, "获取图片数据成功");
		return imgData;
	}
	
	/**
	 * 获取图片数据
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getImgData")
	public @ResponseBody JSONObject getImgData(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getImgData(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("getImgData");
		addMessage(redirectAttributes, "获取图片数据成功");
		return imgData;
	}
	/**
	 * 新增列信息
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/addColumn")
	public @ResponseBody JSONObject addColumn(MlDataModel mlDataModel,FormulaDict formulaDict,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.addColumn(mlDataModel, formulaDict);
		JSONObject imgData = (JSONObject)resp.getResponse().get("addColumn");
		addMessage(redirectAttributes, "获取图片数据成功");
		return imgData;
	}
	@RequestMapping(method = RequestMethod.GET, value = "/replaceColumnsValue")
	public @ResponseBody JSONObject replaceColumnsValue(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.replaceColumnsValue(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("replaceColumnsValue");
		addMessage(redirectAttributes, "列数据替换成功");
		return imgData;
	}
	
	/**
	 * 获取图片数据
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/setTarget")
	public @ResponseBody JSONObject setTarget(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.setTarget(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("setTarget");
		addMessage(redirectAttributes, "获取图片数据成功");
		return imgData;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/getSCcurve")
	public @ResponseBody JSONObject getSCcurve(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getSCcurve(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("getSCcurve");
		addMessage(redirectAttributes, "获取数据成功");
		return imgData;
	}
	@RequestMapping(method = RequestMethod.GET, value = "/getBestKData")
	public @ResponseBody JSONObject getBestKData(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getBestKData(mlDataModel, imgVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("getBestKData");
		addMessage(redirectAttributes, "获取数据成功");
		return imgData;
	}
	
    @RequestMapping(value = "/IoReadImage", method = RequestMethod.GET)  
    public String IoReadImage(String savepath,HttpServletRequest request,HttpServletResponse response) throws IOException {  
    	ServletOutputStream out = null;  
        InputStream ips = null;  
        try {  
        	// 通过URL请求获取图片
    		String imgurl = DOWNLOAD_URL+"?savepath="+savepath;
    		URL url = new URL(imgurl);
            ips = url.openStream();
            response.setContentType("multipart/form-data");  
            out = response.getOutputStream();  
            //读取文件流  
            int len = 0;  
            byte[] buffer = new byte[1024 * 10];  
            while ((len = ips.read(buffer)) != -1){  
                out.write(buffer,0,len);  
            }  
            out.flush();  
        }catch (Exception e){  
            e.printStackTrace();  
        }finally {  
            out.close();  
            ips.close();  
        }  
        return null; 
    }  
}
